package com.zhangyu.shadow.ec.main.personal.order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.tepth.shadow.ui.recycler.BaseDecoration;
import com.zhangyu.shadow.core.delegate.RxShadowDelegate;
import com.zhangyu.shadow.core.net.callback.IError;
import com.zhangyu.shadow.core.net.callback.ISuccess;
import com.zhangyu.shadow.core.net.rx.RxRestClient;
import com.zhangyu.shadow.ec.R;
import com.zhangyu.shadow.ec.R2;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author zhangyu
 * @date 2018/1/4
 */

public class OrderListDelegate extends RxShadowDelegate {
    private static final String ARG_ORDER_TYPE = "orderType";
    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    private int mType;

    public static OrderListDelegate create(int type) {
        final OrderListDelegate contentDelegate = new OrderListDelegate();
        final Bundle bundle = new Bundle();
        bundle.putInt(ARG_ORDER_TYPE, type);
        contentDelegate.setArguments(bundle);
        return contentDelegate;
    }



    @Override
    public Object setLayout() {
        return R.layout.delegate_order_list;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        mType = bundle.getInt(ARG_ORDER_TYPE);
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.addItemDecoration(BaseDecoration.create(ContextCompat.getColor(getContext(),
                R.color.app_background), 1));

        mDisposables.add(RxRestClient.builder()
                .loader(getContext())
                .url("order_list.php")
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        OrderDataConverter converter = new OrderDataConverter();
                        converter.setJsonData(response);
                        OrderListAdapter adapter = new OrderListAdapter(converter.convert());
                        mRecyclerView.setAdapter(adapter);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        Log.e("onError",msg);
                    }
                })
                .build().get());
    }
}
